package com.campus.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.campus.api.util.UserContext;
import com.campus.campus.job.entity.CampusCards;
import com.campus.campus.mapper.CampusCardsMapper;
import com.campus.common.exception.ServiceException;
import com.campus.common.result.Result;
import com.campus.common.student.job.entity.Student;
import com.campus.common.student.mapper.StudentMapper;
import com.campus.campus.job.entity.UtilityConsumptionRecord;
import com.campus.campus.mapper.UtilityConsumptionRecordMapper;
import com.campus.campus.service.IUtilityConsumptionRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 水电费消耗记录表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-30
 */
@Service
public class UtilityConsumptionRecordServiceImpl extends ServiceImpl<UtilityConsumptionRecordMapper, UtilityConsumptionRecord> implements IUtilityConsumptionRecordService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private CampusCardsMapper campusCardsMapper;

    @Autowired
    private UtilityConsumptionRecordMapper utilityConsumptionRecordMapper;

    @Override
    public Result getTodayUsage(String type) {
        int userId = UserContext.getUserId();
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>()
                .eq(Student::getUserId, userId));
        if (Optional.ofNullable(student).isEmpty()) {
            throw new ServiceException("未查询到该学生信息");
        }

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startTime = now.withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endTime = now.withHour(23).withMinute(59).withSecond(59).withNano(999999999);

        CampusCards campusCards=campusCardsMapper.selectOne(new LambdaQueryWrapper<CampusCards>().eq(CampusCards::getStudentId,student.getId()));
        LambdaQueryWrapper<UtilityConsumptionRecord> wrapper = new LambdaQueryWrapper<UtilityConsumptionRecord>()
                .eq(UtilityConsumptionRecord::getCampusCardId, campusCards.getId())
                .eq(UtilityConsumptionRecord::getType, type)
                .ge(UtilityConsumptionRecord::getDeductionTime, startTime)
                .le(UtilityConsumptionRecord::getDeductionTime, endTime);

        List<UtilityConsumptionRecord> list = utilityConsumptionRecordMapper.selectList(wrapper);

        //求总消耗的水费或者电费
        if (Optional.ofNullable(list).isPresent()) {
            BigDecimal total = list.stream()
                    .map(UtilityConsumptionRecord::getDeductionAmount) // 使用 map 转换为 BigDecimal 流
                    .reduce(BigDecimal.ZERO, BigDecimal::add); // 使用 reduce 累加
            return Result.success(total);
        }
        return Result.fail("获取数据失败");
    }
}
